﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// This is used to calculate a difference between two pixels next to each other (horizontal direction)
    /// </summary>
    public class RGBComponentDifference : BaseForeBackgroundColorFilter
    {
        protected int m_nThreshold = 0;
        protected bool m_bOnlyThreshold = false;

        /// <summary>
        /// default constructor
        /// </summary>
        public RGBComponentDifference()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_nThreshold"></param>
        /// <param name="_bOnlyThreshold"></param>
        public RGBComponentDifference(int _nThreshold, bool _bOnlyThreshold)
        {
            m_nThreshold = _nThreshold;
            m_bOnlyThreshold = _bOnlyThreshold;
        }

        /// <summary>
        /// used by clone to create a deep copy
        /// </summary>
        /// <param name="_rgbComponentDifference"></param>
        internal RGBComponentDifference(RGBComponentDifference _rgbComponentDifference)
        {
            this.m_bOnlyThreshold = _rgbComponentDifference.m_bOnlyThreshold;
            this.m_nThreshold = _rgbComponentDifference.m_nThreshold;
        }

        /// <summary>
        /// Gets or sets threshold
        /// </summary>
        public int Threshold
        {
            get { return m_nThreshold; }
            set { m_nThreshold = value; }
        }

        /// <summary>
        /// Gets or sets if only this concrete set threshold should be used
        /// </summary>
        public bool OnlyThreshold
        {
            get { return m_bOnlyThreshold; }
            set { m_bOnlyThreshold = value; }
        }

        /// <summary>
        /// executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;
            List<Color> aColor = new List<Color>();
            int y = 0;
            int x = 0;

            int nCount = 0;
            aColor.Add(bitmap.GetPixel(x, y));
            nCount++;

            int nWidth = bitmap.Width;
            int nHeight = bitmap.Height;

            Color color = Color.Transparent;
            Color colorPrev = Color.Transparent;
            Color colorCurrent = Color.Transparent;
            
            int nDiff = -1;
            int nSumPrev = -1;
            int nSumCurrent = -1;
            
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = bitmap.GetPixel(x, y);
                    aColor.Add(color);
                    colorPrev = aColor[nCount - 1];
                    colorCurrent = aColor[nCount];

                    nSumPrev = ColorFunctions.RGBChannelSum(colorPrev);
                    nSumCurrent = ColorFunctions.RGBChannelSum(colorCurrent);

                    nDiff = Math.Abs(nSumPrev - nSumCurrent);
                    if (m_bOnlyThreshold)
                    {
                        if (nDiff == m_nThreshold)
                        {
                            bitmap.SetPixel(x, y, m_ColorForeground);
                        }
                        else
                        {
                            bitmap.SetPixel(x, y, m_ColorBackground);
                        }
                    }
                    else
                    {
                        if (nDiff >= m_nThreshold)
                        {
                            bitmap.SetPixel(x, y, m_ColorForeground);
                        }
                        else
                        {
                            bitmap.SetPixel(x, y, m_ColorBackground);
                        }
                    }
                    nCount++;
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new RGBComponentDifference(this);
        }
    }
}
